package net.takela.common.web.service.sys;

import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author baboy
 * create at 05/12/2016
 */
@Service("memoryCacheService")
public class MemoryCacheService {
    public static String DEFAULT_NM = "g";
    public static Map<Object,HashMap<Object,CacheItem>> storage;
    static {
        storage = new HashMap();
    }
    /**
     * 
     */
    public void set(Object k, Object v){
        set(k,v,(Date)null);
    }
    /**
     * 
     */
    public void set(Object k, Object v, int expire){
        Date expireDate = null;
        if (expire > 0){
            Date now = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(now);
            calendar.add(Calendar.SECOND, expire);
            expireDate = calendar.getTime();
        }
        set(k,v,expireDate);
    }

    /**
     * main
     * @param k
     * @param v
     * @param expire
     */
    /**
     * 
     */
    public void set(Object k, Object v, Date expire){
        set(DEFAULT_NM, k, v, expire);
    }
    synchronized public void set(Object nm,Object k, Object v, Date expire){
        CacheItem item = new CacheItem(v, expire);
        HashMap<Object,CacheItem> d = storage.get(nm);
        if(d == null){
            d = new HashMap<Object, CacheItem>();
            storage.put(nm, d);
        }
        d.put(k, item);
    }
    /**
     * 
     */
    public void remove(Object k){
        remove(DEFAULT_NM,k);
    }
    synchronized  public void remove(Object nm,Object k){
        HashMap<Object,CacheItem> d = storage.get(nm);
        if(d != null){
            d.remove(k);
        }
    }
    /**
     * 
     */
    public Object get(Object k){
        return get(DEFAULT_NM, k);
    }
    synchronized public Object get(Object nm, Object k){
        HashMap<Object,CacheItem> d = storage.get(nm);
        if(d != null){
            CacheItem item  = d.get(k);
            if (item != null && (
                    item.getExpireDate() == null ||
                    item.getExpireDate().after(new Date()) )
                    ){
                return item.getData();
            }
            if (item != null) {
                remove(nm, k);
            }
        }
        return null;
    }



    public static class CacheItem{
        private Object data;
        private Date expireDate;
    /**
     * 
     */
        public CacheItem(Object data,Date expireData){
            this.data = data;
            this.expireDate = expireData;
        }
    /**
     * 
     */
        public Object getData() {
            return data;
        }
    /**
     * 
     */
        public void setData(Object data) {
            this.data = data;
        }
    /**
     * 
     */
        public Date getExpireDate() {
            return expireDate;
        }
    /**
     * 
     */
        public void setExpireDate(Date expireDate) {
            this.expireDate = expireDate;
        }
    }
}
